#!/bin/sh

#--------------------------------------------------------------------
# mac script commands list - all commands
#--------------------------------------------------------------------

MacCategories=(
general
git
homebrew
lamp
network
dns
performance
search
ssh
terminal
web_utilities
webdev
)

function printInfo()
{
    echo "\n${WHITEBOLD} mac CLI – OS X command line tools for developers"
    echo "====================================================${NC}"
}

function printAdditionalHelp()
{
    echo "\nRun ${LIGHTBLUE}mac help${NC} for full usage, ${LIGHTBLUE}mac categories${NC} for categories list, or ${LIGHTBLUE}mac {category}${NC} for usage related to {category}"
    echo "${NC}\n"
}

function categoriesList()
{
    cats=${cats}$(printf "%s|" "${MacCategories[@]}")
    cats="${cats%?}"
    echo "${cats}"
}

function usageList()
{
    case ${1} in
        general)
            echo "\n${WHITEBOLD}General Utilities: "
            echo "${LIGHTBLUE}mac update${GRAY}: Install OS X software updates, update installed Ruby gems, Homebrew, npm and their installed packages"
            echo "${LIGHTBLUE}mac upgrade${GRAY}: Upgrade Mac CLI to the latest version"
            echo "${LIGHTBLUE}mac uninstall${GRAY}: Uninstall Mac CLI"
            echo "${LIGHTBLUE}mac lock${GRAY}: Lock"
            echo "${LIGHTBLUE}mac restart${GRAY}: Restart OS X"
            echo "${LIGHTBLUE}mac sleep${GRAY}: Sleep mode"
            echo "${LIGHTBLUE}mac shutdown${GRAY}: Shutdown"
            echo "${LIGHTBLUE}mac uptime${GRAY}: Get the time since last restart"
            echo "${LIGHTBLUE}mac volume${GRAY}: Get the volume from the terminal"
            echo "${LIGHTBLUE}mac volume ${LIGHTGREEN}X${GRAY}: Set the volume from the terminal - ${LIGHTGREEN}X = Level (0-100)"
            echo "${LIGHTBLUE}mac volume:mute${GRAY}: Mute volume"
            echo "${LIGHTBLUE}mac volume:unmute${GRAY}: Unmute volume"
            echo "${LIGHTBLUE}mac volume:ismute${GRAY}: Check if the volume is muted or not"
            echo "${LIGHTBLUE}mac screensaver${GRAY}: Start screensaver"
            echo "${LIGHTBLUE}mac folders:list${GRAY}: List folders in current directory with their current size"
            echo "${LIGHTBLUE}mac folder:size${GRAY}: Calculate current folder size"
            echo "${LIGHTBLUE}mac dock:add-space ${LIGHTGREEN}N${GRAY}: Add blank space to dock - ${LIGHTGREEN}N = Number of spaces to add"
            echo "${LIGHTBLUE}mac eject-all${GRAY}: Eject all mounted volumes and disks"
            echo "${LIGHTBLUE}mac battery${GRAY}: Get battery information"
            echo "${LIGHTBLUE}mac info${GRAY}: Get OS X version information"
            echo "${LIGHTBLUE}mac find:text ${LIGHTGREEN}X${GRAY}: Find exact phrase recursively inside directory - ${LIGHTGREEN}X = Text string"
            echo "${LIGHTBLUE}mac find:biggest-files ${GRAY}: Find biggest files inside directory"
            echo "${LIGHTBLUE}mac find:biggest-directories ${GRAY}: Find biggest directories inside directory"
            echo "${LIGHTBLUE}mac zip:extract ${LIGHTGREEN}X${GRAY}: Extract Zip file to current folder - ${LIGHTGREEN}X = Zip file to extract"
            echo "${LIGHTBLUE}mac gzip:compress ${LIGHTGREEN}X${GRAY}: Compress current file using Gzip - ${LIGHTGREEN}X = File to compress"
            echo "${LIGHTBLUE}mac gzip:extract ${LIGHTGREEN}X${GRAY}: Extract Gzip file to current folder - ${LIGHTGREEN}X = Gzip file to extract"
            echo "${LIGHTBLUE}mac tar:compress ${LIGHTGREEN}X${GRAY}: Compress X file/directory using tar with progress indicator - ${LIGHTGREEN}X = File or directory"
            echo "${LIGHTBLUE}mac tar:extract ${LIGHTGREEN}X${GRAY}: Extract tar file to current folder - ${LIGHTGREEN}X = Tar file to extract"
            echo "${NC}\n"
            ;;
        search)
            echo "\n${WHITEBOLD}Search Utilities: "
            echo "${LIGHTBLUE}mac find:recent ${LIGHTGREEN}X${GRAY}: Find files modified in the last N minutes - ${LIGHTGREEN}X = number of minutes "
            echo "${LIGHTBLUE}mac search:replace ${LIGHTGREEN}X${GRAY}: Search and replace string in file - ${LIGHTGREEN}X = File to perform the search and replace operation"
            echo "${NC}\n"
            ;;
        network)
            echo "\n${WHITEBOLD}Network Utilities: "
            echo "${LIGHTBLUE}mac speedtest${GRAY}: Internet connection speed test "
            echo "${LIGHTBLUE}mac ports${GRAY}: List of used ports "
            echo "${LIGHTBLUE}mac ip:local${GRAY}: Get local IP address "
            echo "${LIGHTBLUE}mac ip:public${GRAY}: Get public IP address "
            echo "${NC}\n"
            ;;
        dns)
            echo "\n${WHITEBOLD}DNS Utilities: "
            echo "${LIGHTBLUE}mac dns:list${GRAY}: List DNS server(s) "
            echo "${LIGHTBLUE}mac dns:add${GRAY}: Add DNS server "
            echo "${LIGHTBLUE}mac dns:remove${GRAY}: Remove DNS server "
            echo "${LIGHTBLUE}mac dns:flush${GRAY}: Flush DNS cache "
            echo "${NC}\n"
            ;;
        ssh)
            echo "\n${WHITEBOLD}SSH Utilities: "
            echo "${LIGHTBLUE}mac ssh:download-file ${LIGHTGREEN}X${GRAY}: Download file from remote server through SSH - X = Path of the remote file to download "
            echo "${LIGHTBLUE}mac ssh:download-folder ${LIGHTGREEN}X${GRAY}: Download entire folder from remote server through SSH - X = Path of the remote folder to download "
            echo "${LIGHTBLUE}mac ssh:sync:local ${LIGHTGREEN}X${GRAY}: Sync local folder with remote folder using rsync through SSH (download remote folder to local folder)"
            echo "${LIGHTBLUE}mac ssh:sync:remote ${LIGHTGREEN}X${GRAY}: Path of the remote folder to sync from local folder (upload local folder to remote folder) "
            echo "${LIGHTBLUE}mac ssh:upload ${LIGHTGREEN}X${GRAY}: Upload file to remote server through SSH - X = Path of the file to upload to the remote server "
            echo "${LIGHTBLUE}mac ssh:public-key ${LIGHTGREEN}X${GRAY}: Get public SSH key for local machine "
            echo "${NC}\n"
            ;;
        performance)
            echo "\n${WHITEBOLD}Performance and maintenance Utilities: "
            echo "${LIGHTBLUE}mac system${GRAY}: Show system information to review mac performance "
            echo "${LIGHTBLUE}mac temp${GRAY}: Show temperature, fan and battery statistics "
            echo "${LIGHTBLUE}mac memory${GRAY}: See memory usage sorted by memory consumption "
            echo "${LIGHTBLUE}mac trash:empty${GRAY}: Empty trash"
            echo "${LIGHTBLUE}mac trash:size${GRAY}: Calculate trash size"
            echo "${NC}\n"
            ;;
        git)
            echo "\n${WHITEBOLD}Git Utilities: "
            echo "${LIGHTBLUE}mac git:config${GRAY}: Display local Git configuration "
            echo "${LIGHTBLUE}mac git:open${GRAY}: Open current repository on Github "
            echo "${LIGHTBLUE}mac git:create:branch${GRAY}: Create branch based on current branch "
            echo "${LIGHTBLUE}mac git:branches:date${GRAY}: Get last update date for all branches in current project "
            echo "${LIGHTBLUE}mac git:undo-commit${GRAY}: Undo latest commit "
            echo "${LIGHTBLUE}mac git:log${GRAY}: See latest commits IDs and titles for current branch "
            echo "${LIGHTBLUE}mac git:branch${GRAY}: See all branches "
            echo "${LIGHTBLUE}mac git:branch:rename${GRAY}: Rename Git branch "
            echo "${LIGHTBLUE}mac git:branch:remove-local${GRAY}: Remove local Git branch "
            echo "${LIGHTBLUE}mac git:branch:remove-remote${GRAY}: Remove local and remote Git branch "
            echo "${LIGHTBLUE}mac git:branch${GRAY}: See all branches "
            echo "${LIGHTBLUE}mac git:add-removed${GRAY}: Add removed files to staged files "
            echo "${LIGHTBLUE}mac git:size${GRAY}: Get size for current Git repository "
            echo "${NC}\n"
            ;;
        homebrew)
            echo "\n${WHITEBOLD}Homebrew Utilities: "
            echo "${LIGHTBLUE}mac brew:update${GRAY}: Upgrade Homebrew, installed Homebrew packages, and cleanup "
            echo "${NC}\n"
            ;;
    esac

}


case ${fn} in
    "list"|"help"|"usage")
        echo "$(printInfo)"
        for c in ${MacCategories[@]}
        do
            echo "$(usageList ${c})"
        done
        echo "$(printAdditionalHelp)"
        ;;
    "categories")
        echo "$(printInfo)"
        echo "\n${WHITEBOLD}mac CLI Categories: ${NC}\n"
        for c in "${MacCategories[@]}"
        do
            echo "${WHITEBOLD} >\t$c ${NC}"
        done
        echo "$(printAdditionalHelp)"
        ;;
esac

for c in "${MacCategories[@]}"
do
    if [ "${c}" = "${fn}" ]; then
        echo "$(printInfo)"
        echo "\n${WHITEBOLD}Usage for 'mac ${c}'"
        echo "$(usageList ${c})"
        break
    fi
done
